package com.sinoglobal.ningxia.activity.mico;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Intent;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ListView;
import android.widget.RelativeLayout;

import com.orhanobut.logger.Logger;
import com.sinoglobal.ningxia.R;
import com.sinoglobal.ningxia.activity.base.BaseActivity;
import com.sinoglobal.ningxia.adapter.MicChannelAdapter;
import com.sinoglobal.ningxia.beans.MicChannelListVo;
import com.sinoglobal.ningxia.downdata.imp.RemoteImpl;
import com.sinoglobal.ningxia.frame.FlyApplication;
import com.sinoglobal.ningxia.http.Code;
import com.sinoglobal.ningxia.utils.FlyUtil;
import com.sinoglobal.ningxia.utils.TextUtil;

import java.util.List;

/**
 * @author qiwx
 * @createdate 2014-6-13 下午4:27:47
 * @Description:微视听
 */
@SuppressLint("DefaultLocale")
public class MicroAudioVisualActivity extends BaseActivity implements
        OnClickListener {
    // 频道列表
    private ListView micChannel;
    // 节目单
    private RelativeLayout programCard;
    private Activity mActivity;
    private MicChannelAdapter adapter;
    private ItktAsyncTaskWithDialog<Void, Void, MicChannelListVo> mTask;// 加载数据
    private String cputype;
    private String FLAG = "flag";// 1代表解码播放，2代表webView播放
    private int NDK = 1;// 解码播放
    private int WEB = 2;// webView播放

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        isTemplate = true;
        setContentView(R.layout.micro_audio_visual_activity);
        openChenJInShi();//开启沉浸式
        mActivity = this;
        init();
        getData();

    }

    //请求数据
    private void getData() {

        mTask = new ItktAsyncTaskWithDialog<Void, Void, MicChannelListVo>(
                getString(R.string.micro_loading_data), true, true) {
            @Override
            public MicChannelListVo before(Void... params) throws Exception {
                if (!TextUtil.stringIsNull(FlyApplication.userId))
                    return RemoteImpl.getInstance().getMicChannelList(
                            FlyApplication.userId);
                else
                    return RemoteImpl.getInstance().getMicChannelList(null);
            }

            @Override
            public void after(MicChannelListVo result) {
                if (Code.CODE_SUCCESS == result.getCode()) {
                    setData(result);
                }
            }

            @Override
            public void exception() {
            }
        };
        mTask.execute();
    }

    private void setData(MicChannelListVo vo) {

        adapter = new MicChannelAdapter(this, vo.getList());
        micChannel.setAdapter(adapter);
        programCard.setVisibility(View.VISIBLE);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (mTask != null
                && mTask.getStatus() == AsyncTask.Status.RUNNING) {
            mTask.cancel(true);// 如果Task还在运行，则先取消它
            mTask = null;
        }
    }

    // 检查是否有flash插件
    private boolean checkFlash() {
        PackageManager pm = getPackageManager();
        List<PackageInfo> infoList = pm
                .getInstalledPackages(PackageManager.GET_SERVICES);
        for (PackageInfo info : infoList) {
            if ("com.adobe.flashplayer".equals(info.packageName)) {
                return true;
            }
        }
        return false;
    }

    // 检查系统版本，2.x版本可以直接播放flash，
    private boolean checkSysVer() {
        int version = android.os.Build.VERSION.SDK_INT;
        if (version < 10)
            return true;
        else
            return false;
    }

    private void init() {
        titleView.setText(R.string.ningxia_tv);
//        templateButtonRight.setBackgroundResource(R.drawable.broadcast_icon);

        cputype = android.os.Build.CPU_ABI;
        micChannel = (ListView) findViewById(R.id.micListView);
        programCard = (RelativeLayout) findViewById(R.id.Rela_menu);
        programCard.setOnClickListener(this);
        micChannel.setDivider(null);
        micChannel.setDividerHeight(10);

    }

    @SuppressLint("DefaultLocale")
    @Override
    public void onClick(View v) {
        // 顶部右侧功能按钮
        switch (v.getId()) {
            case R.id.title_but_right:
                Logger.i("点击前往收音机界面");
//                if (AndPermission.hasPermission(MicroAudioVisualActivity.this, Manifest.permission.WRITE_SETTINGS)) {
//                    //有权限
//                    goBroadCastActivity();
//                } else {
//                    AndPermission.with(MicroAudioVisualActivity.this)
//                            .permission(Manifest.permission.WRITE_SETTINGS)
//                            .requestCode(1004)
//                            .send();
//                }

                break;

            // 节目单
            case R.id.Rela_menu:
                Intent go = new Intent();
                go.setClass(mActivity, PlayBillActivity.class);
                FlyUtil.intentForwardNetWork(mActivity, go);
                break;
        }
    }

//    @Override
//    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
//        AndPermission.onRequestPermissionsResult(this, requestCode, permissions, grantResults);
//    }
//
//    // 成功回调的方法，用注解即可，里面的数字是请求时的requestCode。
//    @PermissionYes(1004)
//    private void getLocationYes(List<String> grantedPermissions) {
//        // TODO 申请权限成功。
//        goBroadCastActivity();
//    }
//
//    // 失败回调的方法，用注解即可，里面的数字是请求时的requestCode。
//    @PermissionNo(1004)
//    private void getLocationNo(List<String> deniedPermissions) {
//        // 用户否勾选了不再提示并且拒绝了权限，那么提示用户到设置中授权。
//        if (AndPermission.hasAlwaysDeniedPermission(this, deniedPermissions)) {
//            // 第一种：用默认的提示语。
//            AndPermission.defaultSettingDialog(this, 1004).show();
//        }
//    }

//    /**
//     * 前往广播页
//     */
//    private void goBroadCastActivity() {
//        Intent goBroad = new Intent();
//        goBroad.setClass(mActivity, BroadCastActivity.class);
//        if (cputype == null)
//            return;
//        if (cputype.toLowerCase().contains("arm")) {
//            goBroad.putExtra(FLAG, NDK);
//            FlyUtil.intentForwardNetWork(mActivity, goBroad);
//        }
//        // 3.0之前的系统版本可以直接播放flash
//        else if (checkSysVer()) {
//            goBroad.putExtra(FLAG, WEB);
//            FlyUtil.intentForwardNetWork(mActivity, goBroad);
//        }
//        // 手机本身有flash插件的也可以播放
//        else if (checkFlash()) {
//            goBroad.putExtra(FLAG, WEB);
//            FlyUtil.intentForwardNetWork(mActivity, goBroad);
//        }
//        // 否则提示用户无法播放
//        else {
//            showShortToastMessage(getResources().getString(
//                    R.string.nonSupportRadio));
//        }
//    }
}
